package com.scpii.api.domain.common;

import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.codehaus.jackson.annotate.JsonIgnore;
import org.codehaus.jackson.map.annotate.JsonSerialize;

import com.scpii.api.common.Constants;
import com.scpii.api.common.base.FilterEntity;
import com.scpii.api.common.json.serializer.FieldSerializer;

@JsonSerialize(using = FieldSerializer.class, include = JsonSerialize.Inclusion.NON_NULL)
public class Field extends FilterEntity implements Serializable {

	public static final Map<String, String> FIELD_USER_KEY = new HashMap<String, String>();

	public static final String[] FIELD_USER_KEYS = new String[] { "USER_NAME",
			"USER_AILAS", "PHONE_NUMBER", "EMAIL", "SEX" };

	public static final Field AVATAR = new Field();

	public static final Field WEIBO = new Field();

	static {
		FIELD_USER_KEY.put("用户名", "USER_NAME");
		FIELD_USER_KEY.put("昵称", "USER_AILAS");
		FIELD_USER_KEY.put("电话号码", "PHONE_NUMBER");
		FIELD_USER_KEY.put("邮箱", "EMAIL");
		FIELD_USER_KEY.put("性别", "SEX");

		AVATAR.setName("上传头像");
		AVATAR.setAliasName("上传头像");
		AVATAR.setMetaType(FieldMetaType.IMAGE.getValue());

		WEIBO.setName("微博");
		WEIBO.setAliasName("微博");
		WEIBO.setMetaType("WEIBO");
		FieldMask mask = new FieldMask();
		mask.setOptionImageUrls(new String[] {
				"http://www.sinaimg.cn/blog/developer/wiki/LOGO_32x32.png",
				"http://mat1.gtimg.com/app/opent/images/wiki/resource/weiboicon32.png" });
		mask.setOptionValues(Constants.WEIBO_KEY_ARRAY);
		WEIBO.setMask(mask);
	}

	public enum FieldStatus {

		NOT_SHOW(0),

		SYS_FIELD_SHOW(1),

		ADD_FIELD_SHOW(2),

		ADD_REQUIRED_SHOW(3);

		private final Integer value;

		private FieldStatus(Integer value) {
			this.value = value;
		}

		public static FieldStatus valueOf(int statusCode) {
			for (FieldStatus status : values()) {
				if (status.value == statusCode) {
					return status;
				}
			}
			throw new IllegalArgumentException("No matching constant for ["
					+ statusCode + "]");
		}

		public Integer getValue() {
			return value;
		}

		@Override
		public String toString() {
			return String.valueOf(value);
		}

	}

	public enum FieldMetaType {

		TEXT("TEXT"),

		RADIO("RADIO"),

		CHECKBOX("CHECKBOX"),

		IMAGERADIO("IMAGERADIO"),

		DATE("DATE"),

		ADDRESS("ADDRESS"),

		TEXTAREA("TEXTAREA"),

		NUMBER("NUMBER"),

		EMAIL("EMAIL"),

		IMAGE("IMAGE"),

		WEIBO("WEIBO");

		private final String value;

		private FieldMetaType(String value) {
			this.value = value;
		}

		public String getValue() {
			return value;
		}

		@Override
		public String toString() {
			return String.valueOf(value);
		}

	}

	private Long id;

	@JsonIgnore
	private Long categoriesId;

	private String name;

	private String aliasName;

	private FieldMask mask;

	@JsonIgnore
	private Long appId;

	private String metaType;

	@JsonIgnore
	private Integer status;

	@JsonIgnore
	private String valueStr;

	private String[] values;

	@JsonIgnore
	private Date createDt;

	@JsonIgnore
	private Long createBy;

	@JsonIgnore
	private Date updateDt;

	@JsonIgnore
	private Long updateBy;

	private static final long serialVersionUID = 1L;

	public Field() {
		super();
	}

	public Field(String name, String aliasName, FieldMask mask,
			String metaType, String valueStr) {
		super();
		this.name = name;
		this.aliasName = aliasName;
		this.mask = mask;
		this.metaType = metaType;
		this.valueStr = valueStr;
	}

	public Field(String name, String aliasName, FieldMask mask,
			String metaType, String[] values) {
		super();
		this.name = name;
		this.aliasName = aliasName;
		this.mask = mask;
		this.metaType = metaType;
		this.values = values;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Long getCategoriesId() {
		return categoriesId;
	}

	public void setCategoriesId(Long categoriesId) {
		this.categoriesId = categoriesId;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name == null ? null : name.trim();
	}

	public String getAliasName() {
		return aliasName;
	}

	public void setAliasName(String aliasName) {
		this.aliasName = aliasName == null ? null : aliasName.trim();
	}

	public Long getAppId() {
		return appId;
	}

	public void setAppId(Long appId) {
		this.appId = appId;
	}

	public String getMetaType() {
		return metaType;
	}

	public void setMetaType(String metaType) {
		this.metaType = metaType == null ? null : metaType.trim();
	}

	public Integer getStatus() {
		return status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}

	public String getValueStr() {
		return valueStr;
	}

	public void setValueStr(String valueStr) {
		this.valueStr = valueStr;
	}

	public Date getCreateDt() {
		return createDt;
	}

	public void setCreateDt(Date createDt) {
		this.createDt = createDt;
	}

	public Long getCreateBy() {
		return createBy;
	}

	public void setCreateBy(Long createBy) {
		this.createBy = createBy;
	}

	public Date getUpdateDt() {
		return updateDt;
	}

	public void setUpdateDt(Date updateDt) {
		this.updateDt = updateDt;
	}

	public FieldMask getMask() {
		return mask;
	}

	public void setMask(FieldMask mask) {
		this.mask = mask;
	}

	public Long getUpdateBy() {
		return updateBy;
	}

	public void setUpdateBy(Long updateBy) {
		this.updateBy = updateBy;
	}

	public void setValues(String[] values) {
		this.values = values;
	}

	public String[] getValues() {
		if (this.values != null && this.values.length != 0) {
			return values;
		} else {
			return StringUtils.split(valueStr, ",");
		}
	}

}